﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using emp.Utility;
using emp.SqlServerDAL;
using System.Data.SqlClient;

public partial class SendEmail : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void butSend_Click(object sender, EventArgs e)
    {
        this.butSend.Enabled = false;
        if (this.subject.Text == "xiaoqin.liu")
        {
            //string subject1 = "【重要】 2011奥迪车主增购再购销售线索—请经销商尽快跟进   ";
            //string tempbody = "奥迪400客户服务中心通过信息核实等方式对奥迪现有车主进行外呼调研，从而挖掘增购再购的销售线索；目前潜在客户管理系统中已经积累了部分增购再购的销售线索，请各经销商登陆奥迪潜在用户管理系统 （http://leads.audi.cn），检索项目名称为：“2011奥迪车主增购再购销售线索” ，提取相关的销售线索进行后续跟踪。<br>";
            //tempbody += "希望各经销商予以高度重视，尽快与客户取得联系并促进成交。<br>";
            //tempbody += "感谢各位的配合，恭祝热销！ ";
            //string subject1 = "【重要】 2011奥迪车主增购再购（试乘试驾需求）—请经销商尽快跟进";
            //string tempbody = "奥迪400客户服务中心通过信息核实等方式对奥迪现有车主进行外呼调研，从而挖掘增购再购的需求；目前潜在客户管理系统中已经积累了部分奥迪车主试乘试驾的需求信息，请各经销商登陆奥迪潜在用户管理系统 （http://leads.audi.cn），检索项目名称为：“2011奥迪车主增购再购计划” ，提取相关信息进行后续跟踪。<br>";
            //tempbody += "希望各经销商予以高度重视，尽快与客户取得联系并促进成交。<br>";
            //tempbody += "感谢各位的配合，恭祝热销！ ";
            string subject1 = "奥迪潜在客户管理系统账号开通（转市场总监和销售总监）";
            string tempbody = "";// "尊敬的各位经销商：<br>请各位在04月30日前，提交04月1日至04月30日新增的D级车主数据；<br>为提高奥迪卓越会员提报、审批、管理效率，奥迪CRM开发了“奥迪卓越会员审批管理系统”，并已正式上线使用。<br>从即日起，您可以随时通过系统提报卓越会员，并查看审批情况。<br>具体申报细则，您可登录系统下载使用手册，感谢您的大力支持，恭祝热销！";
         string webpath = "http://leads.audi.cn/";
            try
            {
                // string sqlstr = "select distinct user_uid,user_name,user_email,cc_email  from  system_user_tbl where   user_type='DEALER'  and is_del='0' and user_isagree='Y'and user_uid   in(select ild_current_dealer from INDIVIDUAL_DEALERS where ild_cac_Uid=287 and ild_assign_date>'2011-10-21')";//销售线索
                //  string sqlstr = "select distinct user_uid,user_name,user_email,cc_email  from  system_user_tbl where   user_type='DEALER'  and is_del='0' and user_isagree='Y'and user_uid   in(select ild_current_dealer from INDIVIDUAL_DEALERS where ild_cac_Uid=292 and ild_assign_date>'2011-10-21')";//试乘试驾 
                string sqlstr = "select    user_uid,user_name,login_name,user_password,user_email,cc_email  from  system_user_tbl where user_type='DEALER'  and is_del='0' and user_isagree='Y' and user_uid =" + this.user_id.Text;
;//所有经销商 
                DataTable dealerlist = getTableInfo(sqlstr); //经销商信息列表
                if (dealerlist.Rows.Count > 0)
                {
                    int sum = dealerlist.Rows.Count;
                    for (int i = 0; i < sum; i++)
                    {
                        DataRow drows = dealerlist.Rows[i];
                        bool sendresult = false;
                        string name = drows["user_name"].ToString();
                        string loginname=drows["login_name"].ToString();
                        string password = drows["user_password"].ToString();
                        string email = drows["user_email"].ToString();
                        int user_uid = Int32.Parse(drows["user_uid"].ToString());
                        string cc_email = drows["cc_email"].ToString();

                        tempbody = "尊敬的" + name + "<br>欢迎使用奥迪潜在客户管理系统，现已为贵公司开通系统账号如下：<br>用户名：" + loginname + "<br> 密码：" + password + "<br>操作说明：请登录http://crm.audi.cn/，选择“奥迪潜在客户管理系统”，点击左侧菜单“帮助文档”查看并下载《奥迪潜在客户管理系统经销商使用手册》。";
                        string body = "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\">";
                        body += "<html><head><title>邮件提醒</title><meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\"></head>";
                        body += "<body leftmargin=\"15\" topmargin=\"0\" marginwidth=\"15\" marginheight=\"0\">";
                        body += "<table width=\"803\" border=\"0\" align=\"center\" cellpadding=\"0\" cellspacing=\"0\">";
                        body += " <tr>    <td style=\"border:rgb(0,0,0) 1px solid;\">";
                        body += "<table width=\"801\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">";
                        body += "<tr><td width=\"400\"><img src=\"" + webpath + "/pub/crm/images/email_logo.gif\" alt=\"一汽-大众\" width=\"214\" height=\"117\"></td>"
                            + "<td width=\"401\" align=\"right\"><img src=\"" + webpath + "/pub/crm/images/email_audi.gif\" alt=\"Audi 突破科技 启迪未来\" width=\"353\" height=\"117\"></td> "
                            + "</tr>   </table> ";
                        body += " <table width=\"801\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">  "
                            + "<tr> <td><img src=\"" + webpath + "/pub/crm/images/email_line.gif\" alt=\"\" width=\"801\" height=\"66\"></td>      </tr>     </table>";
                        body += " <table width=\"801\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">       <tr>"
                            + " <td width=\"143\" bgcolor=\"#E5E5E5\"><img src=\"" + webpath + "/pub/crm/images/email_space-g.gif\" alt=\"\" width=\"68\" height=\"68\"></td>"
                            + " <td width=\"515\" bgcolor=\"#E5E5E5\"><span style=\"font-family:Arial, Helvetica, sans-serif; font-size:14px; color:#767676; font-weight: bold;\">"
                            + "尊敬的" + name + "经销商：</span></td> <td width=\"143\" align=\"right\" bgcolor=\"#E5E5E5\"><img src=\"" + webpath + "/pub/crm/images/email_space-g.gif\" alt=\"\" width=\"68\" height=\"68\"></td> "
                            + " </tr></table> ";
                        body += " <table width=\"801\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\"><tr><td><img src=\"" + webpath + "/pub/crm/images/email_space-w.gif\" width=\"25\" height=\"25\"></td></tr></table> ";

                        body += " <table width=\"801\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">   <tr> <td width=\"143\" valign=\"top\">&nbsp;</td> "
                            + "<td width=\"515\"><span style=\"font-family: 宋体, Arial, Helvetica, sans-serif; font-size: 14px; color:#767676; line-height: 36px;\">"
                            + tempbody
                            + "</span></td>    <td width=\"143\" valign=\"top\">&nbsp;</td>       </tr>     </table> ";

                        body += " <p>&nbsp;</p> <p>&nbsp;</p>   <p>&nbsp;</p></td> </tr></table></body></html>";

                        try
                        {

                              sendresult = SendMail.SendMails("leads_system@audi.cn", "奥迪潜在客户管理系统", email, name, cc_email,subject1, body);
                            //    sendresult = SendMail.SendMails("leads_system@audi.cn", "奥迪潜在客户管理系统", "524511020@qq.com,xiaoqin.liu@ogilvy.com", "xiaoqin", subject1, body);
                        }
                        catch (Exception e1)
                        {
                        }
                        string insertsql = "insert into SEND_EMAIL_TEMP(USER_UID,EMAIL,SEND_STATUS,SEND_TIME)values(" + user_uid + ",'" + email + "','N',getdate())";
                        if (sendresult)
                        {
                            insertsql = "insert into SEND_EMAIL_TEMP(USER_UID,EMAIL,SEND_STATUS,SEND_TIME)values(" + user_uid + ",'" + email + "','Y',getdate())";
                        }
                        emp.BLL.Questionnaire qu = new emp.BLL.Questionnaire();
                    qu.executesql(insertsql);
                        this.msg.Text = insertsql;

                    }
                }

            }
            catch (Exception e2)
            {
                msg.Text = e2.StackTrace.ToString();
            }
            this.butSend.Enabled = true;
        }
    }

    DataTable getTableInfo(string sql)
    {
        DataSet data = new DataSet();
        using (SqlDataAdapter sda = new SqlDataAdapter())
        {
            try
            {
                sda.SelectCommand = new SqlCommand();
                sda.SelectCommand.CommandText = sql;
                sda.SelectCommand.Connection = new SqlConnection(SQLHelper.CONN_STRING_NON_DTC);
                sda.Fill(data);
            }
            catch { }
        }
        return data.Tables[0];
    }
}
